<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use App\Models\ProductCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class ProductController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Product(), function (Grid $grid) {
            $grid->model()->orderBy('sort', 'desc');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $data = ProductCategory::query()
                    ->where('parent', '<>', 0)
                    ->where('status', 1)
                    ->pluck('name', 'id')
                    ->toArray();
                $selector->select('category_id', '类型', $data);
            });

            $grid->column('id')->sortable();
            $grid->column('category_id');
            $grid->column('name');
            $grid->column('subtitle')->limit(10);
            $grid->column('main_image')->image('', 50, 50);
            $grid->column('carousel_image')->image('', 50, 50);
            $grid->column('product_sn');
            $grid->column('price');
            $grid->column('sku', '规格')->display('规格查看')->modal(function ($modal) {
                $modal->title('规格');
                if ($this->sku) {
                    $sku = json_decode($this->sku, 1);
                    return admin_view('array', ['data' => $sku['sku']]);
                }
            });
            $grid->column('market_price');
            $grid->column('cost_price');
            $grid->column('stock');
            $grid->column('sold_num');

            $grid->column('weight');
            $grid->column('description')->limit(10);
//            $grid->column('detail');
            $grid->column('status', '上下架')->switch();
            $grid->column('is_hot')->switch();
            $grid->column('is_new')->switch();
            $grid->column('is_recommend')->switch();
            $grid->column('is_farmers', '是否助农')->switch();
            $grid->column('is_specialty', '是否特产')->switch();
            $grid->column('sort')->editable();
//            $grid->column('sku');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('name');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Product(), function (Show $show) {
            $show->field('id');
            $show->field('category_id');
            $show->field('name');
            $show->field('subtitle');
            $show->field('main_image');
            $show->field('carousel_image');
            $show->field('product_sn');
            $show->field('price');
            $show->field('market_price');
            $show->field('cost_price');
            $show->field('stock');
            $show->field('sold_num');
            $show->field('weight');
            $show->field('description');
            $show->field('detail');
            $show->field('status');
            $show->field('is_hot');
            $show->field('is_new');
            $show->field('is_recommend');
            $show->field('sort');
            $show->field('sku');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Product(), function (Form $form) {
            $skuParams = [
                // 扩展第一列
                [
                    'name' => '会员价', // table 第一行 title
                    'field' => 'member_price', // input 的 field_name 名称
                    'default' => 0, // 默认值
                ],
                // 扩展第二列
            ];

            $form->display('id');
            $form->select('category_id')->options(ProductCategory::selectOptions())->required()->required();
            $form->text('name')->required();
            $form->text('subtitle')->required();
            $form->image('main_image')->autoUpload();
            $form->multipleImage('carousel_image')->autoUpload();

            $form->text('product_sn')->required();
            $form->number('price')->required();
            $form->number('market_price')->required();
            $form->number('cost_price')->required();
            $form->number('stock')->required();
            $form->number('sold_num')->required();
            $form->sku('sku', '生成规格')->addColumn($skuParams)->required();
            $form->number('weight')->required();
            $form->textarea('description')->required();
            $form->editor('detail')->required();
            $form->switch('status', '上下架')->default(1)->required();
            $form->switch('is_hot')->default(1)->required();
            $form->switch('is_new')->default(1)->required();
            $form->switch('is_recommend')->default(1)->required();
            $form->switch('is_farmers', '是否助农')->default(1)->required();
            $form->switch('is_specialty', '是否特产')->default(1)->required();

            $form->number('sort')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
